<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.userName" clearable placeholder="请输入学生姓名" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table border :height="height" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="用户头像:">
              <el-image style="width: 60px; height: 60px; background-size: contain" fit="contain" :src="props.row.userAvator" />
            </el-form-item>
            <el-form-item label="学生姓名:">
              <span>{{ props.row.userName }}</span>
            </el-form-item>
            <el-form-item label="家长姓名:">
              <span>{{ props.row.parentName }}</span>
            </el-form-item>
            <el-form-item label="手机号:">
              <span>{{ props.row.userTel }}</span>
            </el-form-item>
            <el-form-item label="学生性别:">
              <span>{{ props.row.sex }}</span>
            </el-form-item>
            <el-form-item label="区域:">
              <span>
                {{ props.row.areaId ? userarea(props.row.areaId) : '' }}
              </span>
            </el-form-item>
            <el-form-item label="初中学校:">
              <span>{{ props.row.areaId ? userSchool(props.row.areaId, props.row.schoolId) : '' }}</span>
            </el-form-item>

            <el-form-item label="语文成绩:">
              <span>{{ props.row.chinese }}</span>
            </el-form-item>
            <el-form-item label="英语成绩:">
              <span>{{ props.row.english }}</span>
            </el-form-item>
            <el-form-item label="数学成绩:">
              <span>{{ props.row.math }}</span>
            </el-form-item>
            <el-form-item label="物理成绩:">
              <span>{{ props.row.physics }}</span>
            </el-form-item>
            <el-form-item label="化学成绩:">
              <span>{{ props.row.chemistry }}</span>
            </el-form-item>
            <el-form-item label="历史成绩:">
              <span>{{ props.row.history }}</span>
            </el-form-item>
            <el-form-item label="地理成绩:">
              <span>{{ props.row.geography }}</span>
            </el-form-item>
            <el-form-item label="生物成绩:">
              <span>{{ props.row.organism }}</span>
            </el-form-item>
            <el-form-item label="体育成绩:">
              <span>{{ props.row.pe }}</span>
            </el-form-item>
            <el-form-item label="道德与法治成绩:">
              <span>{{ props.row.law }}</span>
            </el-form-item>

            <el-form-item label="总分:">
              <span>{{ props.row.totalscore }}</span>
            </el-form-item>

            <el-form-item label="注册时间:">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" prop="id" show-overflow-tooltip />
      <!--   <el-table-column label="用户头像" show-overflow-tooltip width="100">
        <template #default="{ row }">
          <el-image fit="contain" :src="row.userAvator" />
        </template>
      </el-table-column> -->
      <el-table-column label="学生姓名" prop="userName" show-overflow-tooltip />
      <el-table-column label="家长姓名" prop="parentName" show-overflow-tooltip />
      <el-table-column label="手机号" prop="userTel" show-overflow-tooltip />
      <el-table-column label="学生性别" prop="sex" show-overflow-tooltip />
      <el-table-column label="区域" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.areaId ? userarea(scope.row.areaId) : '' }}
        </template>
      </el-table-column>
      <el-table-column label="初中学校" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.areaId ? userSchool(scope.row.areaId, scope.row.schoolId) : '' }}
        </template>
      </el-table-column>
      <el-table-column label="是否指标生" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isTarget == 0 ? 'warning' : 'success'" disable-transitions>
            {{ scope.row.isTarget == 0 ? '否' : '是' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="总分" prop="totalscore" show-overflow-tooltip />
      <el-table-column label="注册时间" prop="createTime" />
      <!-- <el-table-column label="操作" show-overflow-tooltip width="180px">
        <template #default="{ row }">
          <el-button size="mini" type="warning" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { GetAreaSchoolList, SelMiniUser, DelMiniUser } from '@/api/userManagement'
  import Edit from './components/UserManagementEdit'

  export default {
    name: 'UserManagement',
    components: { Edit },
    data() {
      return {
        areaSchoolList: [],
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        Dpmentlist: [],
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          userName: '',
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    async created() {
      let { data } = await GetAreaSchoolList()
      this.areaSchoolList = data
      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      userarea(id) {
        let area = this.areaSchoolList.find((item) => item.id === id)
        return area.label
      },
      userSchool(areaid, schoolid) {
        let area = this.areaSchoolList.find((item) => item.id === areaid)
        let school = area.schoolList.find((item) => item.id === schoolid)
        return school.label
      },
      //添加
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      //编辑
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await DelMiniUser({ userid: row.id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      department(id) {
        if (id) {
          let list = id.split(',').map(Number)
          let dpment = this.Dpmentlist.filter((item) => list.includes(item.id))
          return dpment.map((item) => item.dpName)
        } else {
          return ''
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        SelMiniUser(this.queryForm).then((res1) => {
          this.list = res1.data.records
          this.total = res1.data.total
        })
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>

<style scoped lang="scss">
  .demo-table-expand {
    padding-left: 40px;
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
